<template>
  <div class="sub-filter" v-if="filterData && filterLoading === false">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          :class="{ active: item.id === filterData.brands.selectedBrand }"
          @click="changeBrand(item.id)"
          href="javascript:;"
          v-for="item in filterData.brands"
          :key="item.id"
          >{{ item.name }}</a
        >
      </div>
    </div>
    <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}</div>
      <div class="body">
        <!-- <a href="javascript:;">全部</a> -->
        <a
          :class="{
            active: prop.id === item.selectedAttr
          }"
          @click="changeProp(item, prop.id)"
          href="javascript:;"
          v-for="prop in item.properties"
          :key="prop.id"
          >{{ prop.name }}</a
        >
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '@/api/category'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    // 监听二级类目id的变化 获取筛选数据
    const route = useRoute()
    const filterData = ref(null)
    const filterLoading = ref(false)
    watch(() => route.params.id, (newValue) => {
      // id有值且处在二级类目路由下
      if (newValue && `/category/sub/${newValue}` === route.path) {
        // 发请求获取数据
        findSubCategoryFilter(route.params.id).then(data => {
          // 开始发请求前 ,将filterLoading改成true
          filterLoading.value = true
          // console.log(true)
          // console.log(data.result)
          // 每一组可选的条件缺失全部条件 处理数据 + 全部
          // 给每一组数据加上选中的ID
          // 1.品牌： data.result.brands.unshift({id:null,name:'全部'})
          data.result.brands.selectedBrand = null
          data.result.brands.unshift({ id: null, name: '全部' })
          // console.log(data.result.brands)
          // 2.筛选条件属性 data.result.saleProperties.for
          data.result.saleProperties.forEach((item) => {
            // console.log(item)
            item.selectedAttr = null
            item.properties.unshift({ id: null, name: '全部' })
          })
          // 设置修改的数据
          filterData.value = data.result
          filterLoading.value = false
        })
      }
    }, { immediate: true })
    // 需要有一个获取筛选参数的函数
    const getFilterParams = () => {
      const obj = { brandId: null, attrs: [] }
      // 品牌
      obj.brandId = filterData.value.brands.selectedBrand
      // 销售属性
      filterData.value.saleProperties.forEach(item => {
        if (item.selectedAttr) {
          const prop = item.properties.find(prop => prop.id === item.selectedAttr)
          obj.attrs.push({ groupName: item.name, propertyName: prop.name })
        }
      })
      // 参考数据 {brandId:'',attrs:[{groupName:'',propertyName:'},...]}
      // 当字段为空时 不会发送请求给后台
      if (obj.attrs.length === 0) obj.attrs = null
      return obj
    }
    // 记录当前选择的品牌
    const changeBrand = (brandId) => {
      if (filterData.value.brands.selectedBrand === brandId) {
        return
      }
      filterData.value.brands.selectedBrand = brandId
      emit('filterChange', getFilterParams())
    }
    // 记录选择的销售属性
    const changeProp = (item, propId) => {
      if (item.selectedAttr === propId) return
      item.selectedAttr = propId
      emit('filterChange', getFilterParams())
    }
    return { filterData, filterLoading, changeBrand, changeProp }
  }
}
</script>
<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
  .xtx-skeleton {
    // margin-top: 10px;
    padding: 10px 0;
  }
}
</style>
